// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.chimi.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.chimi.service.BarPresentService;
import com.liuxinlong.modules.dao.BarLuckAwardDao;
import com.liuxinlong.modules.dao.BarPresentDao;
import com.liuxinlong.modules.entity.BarPresent;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 论坛礼品管理服务层接口实现
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-07-28
 */
@Service
@Slf4j
public class BarPresentServiceImpl implements BarPresentService {

    @Autowired
    private BarPresentDao barPresentDao;

    @Autowired
    private BarLuckAwardDao barLuckAwardDao;

    @Override
    public List<Map<String, Object>> pagePresent(Map<String, Object> queryParam) {
        int startNum = (int) queryParam.get("startNum");
        List<BarPresent> originalList = barPresentDao.pageBarPresentList(queryParam);
        return completeResult(originalList,startNum);
    }

    @Override
    public int getPresentCount(Map<String, Object> queryParam) {
        return barPresentDao.getBarPresentCount(queryParam);
    }

    @Override
    public void addPresent(BarPresent barPresent) {
        barPresent.setId(SnowFlake.nextIdStr());
        barPresent.setLeftNum(barPresent.getTotalNum());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        barPresent.setCreateTime(currentTime);
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        barPresent.setCreateUser(currentUser.getId());
        barPresentDao.insert(barPresent);
    }

    @Override
    public void updatePresent(BarPresent barPresent) {
        String id = barPresent.getId();
        if (StringUtils.isEmpty(id)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改礼品信息异常，礼品不存在！");
        }
        BarPresent oldInfo = barPresentDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改礼品信息异常，礼品不存在！");
        }
        if (oldInfo.getStatus() == 1) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改礼品信息异常，礼品已生效！");
        }
        barPresent.setTotalNum(oldInfo.getTotalNum());
        barPresent.setLeftNum(oldInfo.getLeftNum());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        barPresent.setUpdateTime(currentTime);
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        barPresent.setUpdateUser(currentUser.getId());
        barPresentDao.updateById(barPresent);
    }

    @Override
    public void deletePresent(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除礼品信息异常，礼品不存在！");
        }
        BarPresent oldInfo = barPresentDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除礼品信息异常，礼品不存在！");
        }
        if (oldInfo.getStatus() == 1) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除礼品信息异常，礼品已生效！");
        }
        barPresentDao.deleteById(id);
    }

    @Override
    public void updatePresentStatus(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改礼品状态异常，礼品不存在！");
        }
        BarPresent oldInfo = barPresentDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改礼品状态异常，礼品不存在！");
        }
        if (oldInfo.getStatus() == 1) {
            if (barLuckAwardDao.getUsedCount(id) > 0) {
                throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改礼品状态异常，礼品正在使用中，请修改活动！");
            }
        }
        int targetStatus = oldInfo.getStatus() == 0 ? 1 : 0;
        barPresentDao.updatePresentStatus(id,targetStatus);
    }

    @Override
    public List<Map<String, Object>> queryAvailablePresent() {
        List<BarPresent> originList = barPresentDao.queryAvailablePresent();
        List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
        for (BarPresent record : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(record);
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> queryAllPresent() {
        Map<String,Object> queryParam = new HashMap<>();
        queryParam.put("startNum",0);
        queryParam.put("pageSize",10000);
        List<BarPresent> originList = barPresentDao.pageBarPresentList(queryParam);
        List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
        for (BarPresent record : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(record);
            resultList.add(map);
        }
        return resultList;
    }

    /**
     * 数据补全转换
     * @param originList 原始数据列表
     * @param startNum 起始查询数
     * @return 补全数据
     */
    private List<Map<String, Object>> completeResult(List<BarPresent> originList, int startNum) {
        if (ObjectUtils.isEmpty(originList)) {
            return new ArrayList<>();
        }
        List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
        int sort = startNum + 1;
        for (BarPresent record : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(record);
            map.put("sort", sort);
            resultList.add(map);
            sort++;
        }
        return resultList;
    }
}
